package com.taxshare.manager.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.manager.constant.ResultConstant;
import com.taxshare.manager.entity.RoleEntity;
import com.taxshare.manager.mapper.RoleMapper;
import com.taxshare.manager.view.dto.RoleDTO;
import com.taxshare.mybatis.utils.DozerBeanUtil;
import com.taxshare.response.beans.Response;
import com.taxshare.tools.constant.CommonConstant;
import com.taxshare.tools.token.UserInfoUtil;
import com.taxshare.tools.util.UUIDUitl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: RoleService
 * @Description:
 * @author: luozhongyi
 * @date: 2021/1/25 9:41
 */
@Service
public class RoleService extends ServiceImpl<RoleMapper, RoleEntity> {

    /**
     * 新增角色
     *
     * @param roleDTO
     * @return
     */
    public Response add(RoleDTO roleDTO) {
        if (StrUtil.hasBlank(roleDTO.getRoleName())) {
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", roleDTO.getRoleName());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            return Response.error(ResultConstant.RET_R0002_CODE, ResultConstant.RET_R0002_MSG);
        }
        RoleEntity entity = DozerBeanUtil.map(roleDTO, RoleEntity.class);
        entity.setRoleId(UUIDUitl.UUID32(false));
        this.save(entity);
        return Response.success();
    }

    /**
     * 编辑角色
     *
     * @param roleDTO
     * @return
     */
    public Response edit(RoleDTO roleDTO) {
        if (StrUtil.hasBlank(roleDTO.getRoleName())) {
            return Response.error(ResultConstant.RET_R0001_CODE, ResultConstant.RET_R0001_MSG);
        }
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_name", roleDTO.getRoleName());
        queryWrapper.ne("role_id", roleDTO.getRoleId());
        if (baseMapper.selectCount(queryWrapper) > 0) {
            return Response.error(ResultConstant.RET_R0002_CODE, ResultConstant.RET_R0002_MSG);
        }
        RoleEntity entity = DozerBeanUtil.map(roleDTO, RoleEntity.class);
        return Response.success(this.updateById(entity));
    }

    /**
     * 删除角色
     *
     * @param roleDTO
     * @return
     */
    public Response delete(RoleDTO roleDTO) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id", roleDTO.getRoleId());
        baseMapper.delete(queryWrapper);
        return Response.success();
    }

    /**
     * 查询角色
     *
     * @param roleDTO
     * @return
     */
    public Response info(RoleDTO roleDTO) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper();
        if (!StrUtil.hasBlank(roleDTO.getRoleName())) {
            queryWrapper.like("role_name", roleDTO.getRoleName());
        }
        if(!CommonConstant.AUTH_TYPE_3.equals(UserInfoUtil.getUserType())){
            queryWrapper.eq("create_userid",UserInfoUtil.getUserID());
        }
        List<RoleEntity> list = this.list(queryWrapper);
        return Response.success(list);
    }
}
